package icu.sehnen.controller;


import com.github.pagehelper.PageInfo;
import icu.sehnen.common.CommonResult;
import icu.sehnen.mapper.ProgressMapper;
import icu.sehnen.model.CompanyModel;
import icu.sehnen.model.ProCategoryModel;
import icu.sehnen.model.ProductModel;
import icu.sehnen.service.ProCategoryService;
import icu.sehnen.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ProCategoryService proCategoryService;

    @GetMapping("/list")
    public  CommonResult findAll(Integer pageNum,Integer pageSize,String query){
        PageInfo<ProductModel> productModelList=productService.findAll( pageNum, pageSize,query);
        return  CommonResult.success(productModelList);

    }

    @GetMapping("/category")
        public CommonResult getCategory(){
        List<ProCategoryModel> proCategoryModelList= proCategoryService.findAll();
        return  CommonResult.success(proCategoryModelList);
    }
    @DeleteMapping("/delete/{id}")
    public  CommonResult delete(@PathVariable Long id){
       productService.delete(id);
        return  CommonResult.success();
    }
    @GetMapping("/find/{id}")
    public CommonResult findProductById(@PathVariable Long id){
        ProductModel productModel=productService.findProductById(id);
        return  CommonResult.success(productModel);
    }

    @PostMapping("/update")
    public  CommonResult updateProductById(ProductModel productModel){
        productService.update(productModel);
        return  CommonResult.success();

    }
    @PostMapping("/add")
    public  CommonResult add(ProductModel productModel){
      productService.add(productModel);
        return CommonResult.success();

    }


}
